from business.redis.redis_helper import RedisHelper
from typing import Optional, Dict, Any

def find_page_by_name(session_id: str, page_name: str) -> Optional[Dict[str, Any]]:
    """
    根据会话ID和页面名称从Redis中查找对应的菜单项
    
    Args:
        session_id (str): 会话ID
        page_name (str): 页面名称
    
    Returns:
        Optional[Dict[str, Any]]: 找到的菜单项，如果未找到则返回None
    """
    try:
        # 创建Redis助手实例
        redis_helper = RedisHelper()
        
        # 构造Redis key
        redis_key = f"AI_CONTEXT:{session_id}"
        
        # 从Redis获取数据
        context_data = redis_helper.get_json(redis_key)
        
        if not context_data:
            print(f"未找到会话ID为 {session_id} 的上下文数据")
            return None
            
        # 获取菜单列表
        menu_list = context_data.get('menuList', [])
        
        # 查找匹配的菜单项
        for menu_item in menu_list:
            if menu_item.get('menuName') == page_name:
                return menu_item
                
        print(f"在菜单列表中未找到名称为 {page_name} 的页面")
        return None
        
    except Exception as e:
        print(f"查找页面时发生错误: {str(e)}")
        return None
